<?php
/**
 * Description of MDimension class
 *
 * @author Chisheng Lu
 * @copyright DoRISE team, Science Education center, NTNU.
 * @since 2010-03-22
 * @version 1.0
 */

class MDimension {
    private $mysqli;

    function  __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");

    }

    /**
     *
     * @param Integer $intMDimensionId
     * @return Array
     */
    public function getSingleMDimension($intMDimensionId){
        //檢查向度編號是否為空白
        if (empty ($intMDimensionId)) {
            throw new Exception('缺少向度編號！');
        }

        $sql_selMDimension = 'SELECT * FROM `MDimension` WHERE `MDId`=' . $intMDimensionId;
        $rs_selMDimension = $this->mysqli->query($sql_selMDimension);

        if(!$rs_selMDimension){
            throw new Exception('無法查詢單一向度資料！');
        }else{
            if($rs_selMDimension->num_rows == 0){
                throw new Exception('無指定編號的向度資料！');
            }
            $row_selMDimension = $rs_selMDimension->fetch_row();
            return $row_selMDimension;
        }
    }

    /**
     *
     * @return Array
     */
    public function getMDimensionList() {

        $aryMDimensions = array();

        $sql_getMDimensionList = 'SELECT * FROM `MDimension`';
        $rs_getMDimensionList = $this->mysqli->query($sql_getMDimensionList);

        if(!$rs_getMDimensionList){
            throw new Exception('無法查詢向度資料！');
        }else{

            while($row_getMDimensionList = $rs_getMDimensionList->fetch_row()){
                array_push($aryMDimensions, $row_getMDimensionList);
            }

            //釋放查詢結果
            $rs_getMDimensionList->free_result();
        }

        return $aryMDimensions;
    }

    /**
     *
     * @param String $strEngName
     * @param String $strChtName
     * @param Integer $intUserId
     * @return Boolean
     */
    public function addMDimension($strEngName, $strChtName, $intUserId) {
        $bolRtn = false;
        //先查詢是否已有相同名稱
        if(empty ($strEngName) || empty($strChtName)){
            throw new Exception('向度名稱不可為空白！');
        }

        $sql_checkDuplicateValue = 'SELECT * FROM `MDimension` WHERE `MDEngName`=\''.$strEngName.'\' OR `MDChtName`=\''.$strChtName.'\'';
        $rs_checkDuplicateValue = $this->mysqli->query($sql_checkDuplicateValue);

        //檢查是否已有重複值
        if(!$rs_checkDuplicateValue){
            throw new Exception('查詢是否有重複時發生錯誤！');
        }else{
            if($rs_checkDuplicateValue->num_rows != 0){
                throw new Exception('已有相同的向度名稱存在！');
            }else{
                $strNow = $this->getNowTime();
                $sql_addMDimension = 'INSERT INTO `MDimension` SET `MDEngName`=\''.$strEngName.'\', `MDChtName`=\''.$strChtName.'\', `UserId`='.$intUserId. ', `MDCreated`=\''.$strNow.'\'';
                $rs_addMDimension = $this->mysqli->query($sql_addMDimension);
                if(!$rs_addMDimension){
                    throw new Exception('新增向度資料時發生錯誤！');
                }else{
                    $bolRtn = true;
                }
            }
        }

        return $bolRtn;
    }

    /**
     *
     * @param Integer $intMDimensionId
     * @param String $strEngName
     * @param String $strChtName
     * @param Integer $intUserId
     * @return Boolean
     */
    public function editMDimension($intMDimensionId, $strEngName, $strChtName, $intUserId) {
        $bolRtn = false;
        //檢查是否有向度編號
        if(empty ($intMDimensionId)){
            throw new Exception('缺少向度編號！');
        }

        //檢查向度名稱是否為空白
        if(empty ($strEngName) || empty ($strChtName)){
            throw new Exception('向度名稱不可為空白！');
        }

        //更新向度資料
        $strNow = $this->getNowTime();
        $sql_uptMDimension = 'UPDATE `MDimension` SET `MDEngName`=\''.$strEngName.'\', `MDChtName`=\''.$strChtName.'\', `UserId`='.$intUserId. ', `MDModified`=\''.$strNow.'\' WHERE `MDId`='.$intMDimensionId;
        if(!$this->mysqli->query($sql_uptMDimension)){
            throw new Exception('更新向度資料時發生錯誤！');
        }else{
            $bolRtn = true;
        }

        return $bolRtn;
    }

    /**
     *
     * @param Integer $intMDimensionId
     * @return Boolean
     */
    public function delMDimension($intMDimensionId) {
        $bolRtn = false;
        //檢查向度編號是否為空白
        if (empty ($intMDimensionId)) {
            throw new Exception('缺少向度編號！');
        }
        //檢查向度是否為使用中，使用中不允許刪除
        $sql_chkUsed = 'SELECT * FROM `SQMCDIM` WHERE `DimCode`=' . $intMDimensionId;
        $rs_chkUsed = $this->mysqli->query($sql_chkUsed);
        if(!$rs_chkUsed){
            throw new Exception('異常！查詢向度是否為使用中時發生錯誤。');
        }else{
            if($rs_chkUsed->num_rows > 0){
                throw new Exception('向度項目正在使用中，不允許刪除。');
            }else{
                //非使用中的學科，先刪除該向度下類別在刪除向度本身
                $sql_delType = 'DELETE FROM `MDimType` WHERE `MDId`=' . $intMDimensionId;
                if(!$this->mysqli->query($sql_delType)){
                    throw new Exception('錯誤！刪除向度下的類別時發生錯誤。');
                }else{
                    //刪除學科下主題後再刪除該學科
                    $sql_delMDimension ='DELETE FROM `MDimension` WHERE `MDId`=' . $intMDimensionId;
                    if(!$this->mysqli->query($sql_delMDimension)){
                        throw new Exception('錯誤！已刪除向度下層級，於刪除向度時發生錯誤。');
                    }else{
                        $bolRtn = true;
                    }
                }
            }
        }

        return $bolRtn;
    }

    /**
     *
     * @return String
     */
    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    /**
     *
     * @param Integer $intMDimensionId
     * @return Integer
     */
    public function getTypeTotalNumById($intMDimensionId){
        $intTypeCountNum = 0;

        if (empty ($intMDimensionId)) {
            throw new Exception('缺少向度編號！');
        }

        $sql_getTypeNum = 'SELECT Count(*) FROM `SDimType` WHERE `MDId`=' . $intMDimensionId;
        $rs_getTypeNum = $this->mysqli->query($sql_getTypeNum);
        if(!$rs_getTypeNum){
            throw new Exception('查詢向度下層級數時發生錯誤！');
        }else{
            $row_getTypeNum = $rs_getTypeNum->fetch_row();
            $intTypeCountNum = $row_getTypeNum[0];
        }

        return $intTypeCountNum;
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
